import logging
import sys
import ConfigParser
import os

def logger(log_conf_file='conf/log4p.properties',rootlogger='rootlogger'):
        if log_conf_file != None and os.path.exists(log_conf_file):
            config_parser = ConfigParser.RawConfigParser()
            config_parser.read(log_conf_file)

            filename = config_parser.get(rootlogger, 'filename')
            if filename == 'None': filename = None
            if not config_parser.has_section(rootlogger):
                basic_logger()
            else:
                if config_parser.has_option(rootlogger, 'filemode'):
                    filemode = config_parser.get(rootlogger, 'filemode')

                format = config_parser.get(rootlogger, 'format')
                datefmt = config_parser.get(rootlogger, 'datefmt')
                level = {'notset':logging.NOTSET,
                            'debug':logging.DEBUG,
                            'warning':logging.WARNING,
                            'error':logging.ERROR}[config_parser.get(rootlogger, 'level')]
                stream = {'stdout':sys.stdout}[config_parser.get(rootlogger, 'stream')]
                logging.basicConfig(level=level,
                            format= format,
                            datefmt= datefmt,
                            filename= filename,
                            filemode= filemode)
        else:
            basic_logger()
        return logging.getLogger()

def basic_logger():
    logging.basicConfig(level=logging.DEBUG,
        format= '%(filename)s %(funcName)s %(asctime)s %(levelname)s %(message)s',
        datefmt= '%a, %d %b %Y %H:%M:%S',
        stream = sys.stdout)
